<template>
  <div class="container">
    <div class="title">{{ title }}</div>
    <div class="list">
      <div
        v-for="(item, index) in items"
        :key="index"
        class="item"
        draggable
        @dragstart="dragStart(index)"
        @dragover="dragOver(index)"
        @dragend="dragEnd"
      >
        <input v-model="item.text" @change="updateItem(index)" />
        <button @click="deleteItem(index)">删除</button>
      </div>
    </div>
    <button @click="addItem">添加</button>&nbsp;&nbsp;
    <button @click="saveMethod">保存</button>
  </div>
</template>

<script>
export default {
  props: ["items", "title", "saveMethod"],
  data() {
    return {
      // items: [
      //   { text: "列表项1" },
      //   { text: "列表项2" },
      //   { text: "列表项3" },
      //   { text: "列表项4" },
      // ],
      dragIndex: null,
    };
  },
  methods: {
    save() {},
    deleteItem(index) {
      this.items.splice(index, 1);
    },
    updateItem(index) {
      // 可以在这里进行修改操作
    },
    addItem() {
      this.items.push({ text: "新的列表项" });
    },
    dragStart(index) {
      this.dragIndex = index;
    },
    dragOver(index) {
      if (this.dragIndex === null) return;
      const draggedItem = this.items[this.dragIndex];
      this.items.splice(this.dragIndex, 1);
      this.items.splice(index, 0, draggedItem);
      this.dragIndex = index;
    },
    dragEnd() {
      this.dragIndex = null;
    },
  },
};
</script>

<style scoped>
.container {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 10px;
  background-color: #fff;
}
.title {
  font-size: 2em;
  font-weight: bold;
}
.list {
  border: 1px dashed #ccc;
  padding: 10px;
  margin-bottom: 10px;
}

.item {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

input {
  margin-right: 5px;
}

button {
  background-color: #ccc;
  color: #fff;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
}

button:hover {
  background-color: #555;
}
</style>    